package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
    implements BaseAttrInfoService{

    @Autowired
    BaseAttrValueService baseAttrValueService;

    @Override
    public List<BaseAttrInfo> getBaseAttrAndValue(Long category1Id, Long category2Id, Long category3Id) {
        return baseMapper.getBaseAttrAndValue(category1Id,category2Id,category3Id);
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1、将属性名信息保存到base_attr_info中
        save(baseAttrInfo);
        //得到上一步保存到属性的自增id
        Long id = baseAttrInfo.getId();

        //2、将属性值信息保存到base_attr_value
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue baseAttrValue : attrValueList) {
            //回填属性id
            baseAttrValue.setAttrId(id);
//            baseAttrValueService.save(baseAttrValue);
        }
        baseAttrValueService.saveBatch(attrValueList);
    }

    @Override
    public void updateAttrInfo(BaseAttrInfo baseAttrInfo) {

        //修改属性名
        updateById(baseAttrInfo);

        //修改属性值（新增、删除、修改）

        //1、删除  前端没提交的就是删除
        List<Long> ids = new ArrayList<>();
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue baseAttrValue : attrValueList) {
            //id不为空，表示原有属性值，需要进行修改操作
            if(!StringUtils.isEmpty(baseAttrValue.getId())) {
                ids.add(baseAttrValue.getId());
            }
        }

        //原有数据库ids:60、61、62
        //前端传的数据ids：ids:60、62
        //要删除的属性值ids：61  （差集）
        if(ids.size() > 0) {
            baseAttrValueService
                    .lambdaUpdate() //修改删除
                    .eq(BaseAttrValue::getAttrId, baseAttrInfo.getId())
                    .notIn(BaseAttrValue::getId, ids )
                    .remove();
        }else {
            //删除所有
            baseAttrValueService
                    .lambdaUpdate()
                    .eq(BaseAttrValue::getAttrId, baseAttrInfo.getId())
                    .remove();
        }

        //修改base_attr_value
        for (BaseAttrValue baseAttrValue : attrValueList) {
            if(StringUtils.isEmpty(baseAttrValue.getId())) {
                //这些是新增
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueService.save(baseAttrValue);
            }else {
                //这些是修改,属性值提交id就是修改
                baseAttrValueService.updateById(baseAttrValue);
            }
        }
    

    }

}






